package com.globallogic.vacationplanner.web.context;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.globallogic.vacationplanner.transfer.context.IServiceContext;
import com.globallogic.vacationplanner.transfer.service.IProjectService;
import com.globallogic.vacationplanner.transfer.service.IUserService;
import com.globallogic.vacationplanner.transfer.service.IVacationService;

@Component
public class BeanProvider implements IServiceContext, ApplicationContextAware {
	private static final long serialVersionUID = 1L;

	private static Logger LOGGER = Logger.getLogger(BeanProvider.class);
	private IUserService userService;
	private IProjectService projectService;
	private IVacationService vacationService;
	private static ApplicationContext context;

	public BeanProvider() {
		LOGGER.info("BeanProvider has been created");
	}

	@Override
	public IUserService getUserService() {
		return userService;
	}

	@Override
	public IVacationService getVacationService() {
		return vacationService;
	}

	@Override
	public IProjectService getProjectService() {
		return projectService;
	}

	@Autowired
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Autowired
	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}

	@Autowired
	public void setVacationService(IVacationService vacationService) {
		this.vacationService = vacationService;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		BeanProvider.context = applicationContext;
	}

	public static IServiceContext getServiceContext() {
		return (IServiceContext) context.getBean("beanProvider");
	}
}
